package org.zjx.swing.controller;

import org.zjx.swing.model.RecognitionModel;
import org.zjx.swing.view.RecognitionView;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class RecognitionController {
    private final RecognitionModel model;
    private final RecognitionView view;

    public RecognitionController(RecognitionModel model, RecognitionView view) {
        this.model = model;
        this.view = view;

        // 初始化视图
        view.setLargeDisplayText(model.getCurrentWord());

        // 注册按钮事件
        view.addPreviousButtonListener(new PreviousButtonListener());
        view.addNextButtonListener(new NextButtonListener());
        view.addMergeButtonListener(new MergeButtonListener());
        view.addOkButtonListener(new OkButtonListener());
    }

    // 上一个按钮事件
    private class PreviousButtonListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            model.previousWord();
            view.setLargeDisplayText(model.getCurrentWord());
        }
    }

    // 下一个按钮事件
    private class NextButtonListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            model.nextWord();
            view.setLargeDisplayText(model.getCurrentWord());
        }
    }

    // 合并按钮事件
    private class MergeButtonListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            String inputText = view.getInputText();
            model.mergeWords(inputText);
        }
    }

    // 确认按钮事件
    private class OkButtonListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            String inputText = view.getInputText();
            if (!inputText.isEmpty()) {
                view.appendSmallDisplayText(inputText);
                view.clearInputField();
            } else {
                JOptionPane.showMessageDialog(view, "请输入有效内容", "提示", JOptionPane.WARNING_MESSAGE);
            }
        }
    }
}
